home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10135 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: waldorf.csc.calpoly.edu!galaxy!ostoll
  2. From: ostoll@galaxy.csc.calpoly.edu (Oliver Stoll)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: for (int i(1); ...)
  5. Date: 6 Mar 1996 07:22:38 GMT
  6. Organization: Cal Poly Computer Science Dept.
  7. Message-ID: <4hjefu$sbd@waldorf.csc.calpoly.edu>
  8. References: <4gg2j6$93g@darkstar.UCSC.EDU> <ltwx5fuqn8.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. NNTP-Posting-User: ostoll@galaxy.csc.calpoly.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Enno Sandner (enno@inferenzsysteme.informatik.th-darmstadt.de) wrote:
  13. : In article <4gg2j6$93g@darkstar.UCSC.EDU> ray@cse.ucsc.edu (Ray Swartz) writes:
  14.  
  15. :    I noticed that the syntax
  16.  
  17. :    for (int i(1); ...)
  18.  
  19. :    works just like
  20.  
  21. :    for (int i = 1; ...)
  22.  
  23.  
  24. :    however, I've not seen this used anywhere.  
  25.  
  26. :    This seems odd to me since initializing by assignment is seen as misleading
  27. :    when applied to objects.  
  28.  
  29. :    Should 
  30.  
  31. :    for (int i(1);...)
  32.  
  33.  
  34. :    even be used?
  35.  
  36. : That's just a matter of taste, because both solutions are exactly the same.
  37. : In general an expression like 'AClass A_obj=another_A_obj;' will perform a
  38. : copy-construction of an 'AClass' object and won't result in construction +
  39. : subsequent assignment.
  40.  
  41. :     Enno
  42.  
  43. Actually, for the trivial case of an integer, yes. If you should have an
  44. iterator for a class which for example contains a list and switches over the
  45. list with ++ and there is a lot of memory allocation involved, i would well
  46. prefer the copyconstruction of it rather than an assignment. In the case of
  47. an assignment, both destructor and copy constructor have to be called.
  48.  
  49. At htis point i just wanted to mention another incompatibility I have
  50. noticed. There is a difference between AIX's xlC and g++ as for that
  51. definition (so if you want to write portable code, define the variable at
  52. the beginning of the function;):
  53.  
  54. In xlC the scope of a variable defined in for(int i = 0, ...) is the
  55. complete function the for() statement is contained in. In g++ however, the
  56. validity of the int declared like that is only the for statement. So if you
  57. write a program on AIX using i after the for statement to decide the end
  58. vlue of the for perhaps, and compile the program on xlC it will report you
  59. an undefined variable. And if you use the same i with for(int i...) in two
  60. subsequent for calls with g++ AIX will tell you in the second for statement
  61. that i is already defined so...
  62.  
  63. don't use for(int i...), either way ;)
  64.  
  65.  
  66. Oliver
  67.  
  68.  
  69.  
  70. -------------------------------------------------------------------------------
  71.                         Oliver "Oyl McDoyle" Stoll
  72.  
  73.  Fachhochschule Karlsruhe                       California Polytechnic State
  74.  stol0012@fh-karlsruhe.de                       University (San Luis Obispo)
  75.  http://www.fh-karlsruhe.de/~stol0012           ostoll@galaxy.csc.calpoly.edu
  76.